<?php
namespace Test;

use Closure;
use Dybasedev\Dataman\Capsule\Manager as DB;
use Illuminate\Support\Fluent;

class ConvertClass extends Convert
{
    public static $store;

    public function convert($school, $schoolId, $groupinfo, $spaceid)
    {
        // 该学校班级列表
        $classes = $this->connection->table('yuanbao_v1.t_class')
            ->where('kindergartenId', '=', $school->id)
            ->where('isEnable', '=', 1)
            ->get();

        // 旧的班级数据
        // 由于存在不同班级班主任相同的情况，因此需要筛选
        // 记录已经添加的班主任数据（code，name等必要检索信息）
        $added = [];
        foreach ($classes as $class) {
            if (empty($class->classTeacherCode)) {
                continue;
            }

            // 班主任数据转换 
            if (!isset($added[$class->classTeacherCode])) {
                $check = [
                    'ROLE_DIRECTOR' => '校长',
                    'ROLE_TEACHER' => '老师',
                    'ROLE_PARENT' => '家长',
                    'ROLE_DOCTOR' => '医生'
                ];

                $sql = "
SELECT
    t_teacher.*, t_user.*, t_user_role.roleCode
FROM
    yuanbao_v1.t_teacher
JOIN (
    SELECT
        email,
        `password`,
        onlineTime,
        loginNum,
        username
    FROM
        yuanbao_v1.t_user
) AS t_user ON t_user.username = t_teacher.teacherCode
JOIN (
    SELECT
        username,
        roleCode
    FROM
        yuanbao_v1.t_user_role
) AS t_user_role ON t_user_role.username = t_teacher.teacherCode
WHERE
    teacherCode = ?
";

                $oldheadmaster = new Fluent($this->connection->select($sql, [$class->classTeacherCode])[0]);
                $new = array(
                    'user' => $oldheadmaster->teacherCode,
                    'name' => $oldheadmaster->name,
                    'space' => $spaceid,
                    'password' => $oldheadmaster->password,
                    'email' => empty($oldheadmaster->email) ? '' : $oldheadmaster->email,
                    'sex' => $oldheadmaster->sex,
                    'telphone' => empty($oldheadmaster->mobile) ? '' : $oldheadmaster->mobile,
                    'avatar' => empty($oldheadmaster->img) ? '' : $oldheadmaster->img,
                    'created_time' => strtotime($oldheadmaster->createDate),
                    'update_time' => empty($oldheadmaster->modifyDate) ? 0 : strtotime($oldheadmaster->modifyDate),
                    'user_type' => 1,
                    'nickname' => empty($oldheadmaster->nickName) ? '' : $oldheadmaster->nickName,
                    'login_count' => $oldheadmaster->loginCount ?: 0,
                    'chat_id' => $oldheadmaster->teacherCode
                );

                printf("  test data: %s, role data: %s\n", $oldheadmaster->teacherCode, $oldheadmaster->roleCode);
                $group = $check[$oldheadmaster->roleCode];
                foreach ($groupinfo as $value) {
                    if ($value['group'] == $group) {
                        $value['select'] = true;
                        $append[] = $value;
                        break;
                    }
                }

                $added[$class->classTeacherCode] = Tester::addUser($this->connection, $new, $append);

            }

            $data = array(
                'name' => $class->name,
                'school_id' => $schoolId,
                'created_time' => strtotime($class->createDate),
                'update_time' => empty($class->modifyDate) ? 0 : strtotime($class->modifyDate),
                'space' => $spaceid,
                'status' => 1,
                'headmaster_id' => $added[$class->classTeacherCode],
                'register_time' => 0
            );

            // 创建新数据库内的班级
            $classid = $this->connection->table('convert.v2_school_class')->insertGetId($data);
            printf("  Create class from old data, new id is [%d]\n", $classid);

            // 添加班级学生数据
            (new ConvertStudent($this->connection))->convert($school, $schoolId, $groupinfo, $spaceid, $classid, $class);
//            (new ConvertCourse($this->connection))->convert($school, $schoolId, $groupinfo, $spaceid, $classid, $class);
            (new ConvertGrowRecord($this->connection))->convert($school, $schoolId, $groupinfo, $spaceid, $classid, $class);

        }
    }
}